package cn.steel.controller.supplier;

/**
 * @author: zhaoyuhan
 * @date: 2025/4/28
 * @description: 供应商控制器定义
 */
import cn.steel.domain.vo.OrderDetailVo;
import cn.steel.domain.vo.OrderListVo;
import cn.steel.service.OrderService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/supplier/order")
public class SupplierOrderController {

    @Autowired
    private OrderService orderService;

    @ApiOperation("供应商订单列表")
    @GetMapping("/list")
    public String orderList(Model model,
                            @RequestAttribute Long supplierId,
                            @RequestParam(required = false) String status,
                            @RequestParam(required = false) String orderNo,
                            @RequestParam(defaultValue = "1") int pageNum,
                            @RequestParam(defaultValue = "10") int pageSize) {

        // 调用服务层方法，传入分页参数
        PageInfo<OrderListVo> pageInfo = orderService.getSupplierOrderList(supplierId, status, orderNo, pageNum, pageSize);

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("orders", pageInfo.getList());
        model.addAttribute("status", status);
        model.addAttribute("orderNo", orderNo);

        return "supplier/order_list";
    }
    @ApiOperation("供应商订单详情")
    @GetMapping("/detail/{orderNo}")
    public String orderDetail(Model model, @PathVariable String orderNo,
                              @RequestAttribute Long supplierId) {
        OrderDetailVo order = orderService.getSupplierOrderDetail(orderNo, supplierId);
        model.addAttribute("order", order);
        return "supplier/order_detail";
    }
    @ApiOperation("发货")
    @PostMapping("/ship/{orderNo}")
    public String shipOrder(
            @PathVariable String orderNo,
            @RequestParam String shippingCompany,
            @RequestParam String trackingNumber,
            @RequestParam String shippingAddress,
            @RequestAttribute Long supplierId) {

        orderService.shipOrder(orderNo, shippingCompany, trackingNumber, shippingAddress, supplierId);
        return "redirect:/supplier/order/detail/" + orderNo;
    }
    @ApiOperation("删除订单")
    @PostMapping("/delete/{orderNo}")
    public String deleteOrder(@PathVariable String orderNo,
                              @RequestAttribute Long supplierId) {
        orderService.deleteSupplierOrder(orderNo, supplierId);
        return "redirect:/supplier/order/list";
    }
}
